//201902104003邵潇漩
package bysj.dao;

import bysj.domain.*;
import bysj.service.StudentClassService;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class StudentDao {
    private static StudentDao studentDao = null;
    private StudentDao() {
    }

    public static StudentDao getInstance() {
        if (studentDao == null){
            studentDao = new StudentDao();
        }
        return studentDao;
    }

    public static Collection<Student> findAll(Connection connection , List<Condition> conditionList, Pagination pagination) throws SQLException {
        Collection<Student> students = new TreeSet<Student>();
        int totalNum = CommonDao.getInstance().count(connection,"student");
        //创建查询的主语句
        StringBuilder sql_select = new StringBuilder("SELECT * FROM student");
        //如果有条件，则生成条件子句，并附加到主语句后面
        if(conditionList!=null){
            String whereClause = Condition.toWhereClause(conditionList);
            sql_select.append(whereClause);
        }
        if(pagination!=null){
            String LimitClause = pagination.toLimitClause(totalNum);
            sql_select.append(LimitClause);
        }

        //获得查询的预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(sql_select.toString());
        //执行sql查询语句并获得结果集对象
        ResultSet resultSet = pstmt.executeQuery();
        //遍历结果集，根据元组创建对象，并添加到集合中
        while(resultSet.next()){
            students.add(new Student(resultSet.getInt("id"),
                    resultSet.getString("name"),
                    resultSet.getString("no"),
                    resultSet.getString("remarks"),
                    StudentClassService.getInstance().find(resultSet.getInt("studentClass_id"))));
        }
        JdbcHelper.close(resultSet,pstmt);
        return students;
    }

    public static Collection<Student> findAll(Connection connection , Teacher teacher, Pagination pagination) throws SQLException {
        Collection<Student> students = new TreeSet<Student>();
        int totalNum = CommonDao.getInstance().count(connection,"student");
        //创建查询的主语句
        StringBuilder sql_select = new StringBuilder("SELECT * FROM student where teacherId=?");
        //sql_select.append("where teacherId=?");
        //如果有条件，则生成条件子句，并附加到主语句后面
        if(pagination!=null){
            String LimitClause = pagination.toLimitClause(totalNum);
            sql_select.append(LimitClause);
        }
        //获得查询的预编译语句对象
        PreparedStatement pstmt = connection.prepareStatement(sql_select.toString());
        pstmt.setInt(1,teacher.getId());
        //执行sql查询语句并获得结果集对象
        ResultSet resultSet = pstmt.executeQuery();
        //遍历结果集，根据元组创建对象，并添加到集合中
        while(resultSet.next()){
            students.add(new Student(resultSet.getInt("id"),
                    resultSet.getString("name"),
                    resultSet.getString("no"),
                    resultSet.getString("remarks"),
                    StudentClassService.getInstance().find(resultSet.getInt("studentClass_id"))
            ));
        }
        JdbcHelper.close(resultSet,pstmt);
        return students;
    }

    public Student find(Connection conn,Integer id) throws SQLException {
        Student desiredStudent = null;
        String sql_select_id="SELECT*FROM Student WHERE id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql_select_id);
        pstmt.setInt(1,id);
        ResultSet resultSet=pstmt.executeQuery();
        if(resultSet.next()){
            String name=resultSet.getString("name");
            String no = resultSet.getString("no");
            String remarks = resultSet.getString("remarks");
            int studentClassId=resultSet.getInt("studentClass_id");
            desiredStudent=new Student(id,name,no,remarks,StudentClassService.getInstance().find(studentClassId));
        }
        JdbcHelper.close(resultSet,pstmt);
        return desiredStudent;
    }
    public boolean add(Connection conn, Student student) throws SQLException {
        String addStudent_sql="INSERT INTO student "+"(id,name,no,remarks,studentClass_id,teacherId)value(?,?,?,?,?,?)";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = conn.prepareStatement(addStudent_sql);
        //为预编译参数赋值
        pstmt.setInt(1,student.getId());
        pstmt.setString(2, student.getName());
        pstmt.setString(3, student.getNo());
        pstmt.setString(4, student.getRemarks());
        pstmt.setInt(5,student.getStudentClass().getId());
        pstmt.setInt(6,student.getSupervisor().getId());
        int rowAffected =pstmt.executeUpdate();
        JdbcHelper.close(null,pstmt);
        return rowAffected>0;
    }
    public boolean update(Connection conn,Student student) throws SQLException {
        //创建sql语句
        String updateStudent_sql = "UPDATE student SET name=?, no=?, remarks=?, studentClass_id=?,teacherId=? WHERE id=?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = conn.prepareStatement(updateStudent_sql);
        // 为预编译参数赋值
        pstmt.setString(1,student.getName());
        pstmt.setString(2,student.getNo());
        pstmt.setString(3,student.getRemarks());
        pstmt.setInt(4,student.getStudentClass().getId());
        pstmt.setInt(5,student.getSupervisor().getId());
        pstmt.setInt(6,student.getId());
        //执行预编译语句的executeUpdate方法,获得修改的记录行数
        int affectedRowNum=pstmt.executeUpdate();
        //关闭连接
        JdbcHelper.close(null,pstmt);
        //返回school对象是否修改成功
        return affectedRowNum>0;
    }
    public boolean delete(Connection conn,Integer id) throws SQLException {
        //创建sql语句
        String deleteStudent_sql = "DELETE FROM student WHERE id=?";
        //在该连接上创建预编译语句对象
        PreparedStatement pstmt = conn.prepareStatement(deleteStudent_sql);
        //为预编译参数赋值
        pstmt.setInt(1, id);
        //执行预编译语句的executeUpdate方法,获得删除的记录行数
        int affectedRowNum=pstmt.executeUpdate();
        //关闭连接
        JdbcHelper.close(null,pstmt);
        //如果删除记录的行数不为0，返回true,否则返回false
        return affectedRowNum>0;
    }
    public boolean delete(Connection connection,Student student) throws SQLException {
        return this.delete(connection,student.getId());
    }
}
